// pages/teacher/detail/index.js

import {TeacherModel} from "../../../models/teacher/TeacherModel";

const teacherModel = new TeacherModel()
Page({

    /**
     * 页面的初始数据
     */
    data: {

        teacherDetail: {},
        recommendTeacherList: [],
        teacherSubject: [],
        tId: 20174565,

        current: 1,
        pageSize: 10

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        let tId = options.tId
        this.data.tId = tId
        this.init(tId, this.data.current, this.data.pageSize)

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        wx.vibrateShort()
        this.init(this.data.tId, this.data.current, this.data.pageSize)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },


    /**
     * 初始化
     * @param tId
     * @param current
     * @param pageSize
     */
    init(tId, current, pageSize) {

        const teacherDetail = teacherModel.getTeacherDetail(tId)
        const teacherRe = teacherModel.getRecommendTeacher(tId, current, pageSize)
        const teacherSubject = teacherModel.getTeacherSubject(tId, current, pageSize)

        Promise.all([teacherDetail, teacherRe, teacherSubject])

            .then(res => {

                this.setData({
                    teacherDetail: res[0],
                    recommendTeacherList: res[1],
                    teacherSubject: res[2]
                })

                wx.stopPullDownRefresh()
                // wx.xp.hideLoading()

            })
            .catch(err => {
                console.log(err);
            })

    },

    moreTap(event) {
        console.log(event);
    },

    /**
     * 点击课程
     * @param event
     */
    subjectTap(event) {
        let cId = event.currentTarget.dataset.cid;
        wx.navigateTo({
            url: '../../subject/detail/index?cId=' + cId
        })
    }


})